﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Datalib;
using System.Security.Principal;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Forms;

namespace Host
{
    class Program
    {

        static void Main(string[] args)
        {
            if (args.Count() > 0)
            {
                Datalib.Vars.Verbose = args.Contains("-v");

                if (args.Contains("-h") || args.Contains("-?"))
                {
                    printHelp(Application.ExecutablePath);
                    return;
                }
            }

            if (restartAsAdmin(args))
                return;

            if(Datalib.Vars.Verbose)
                Console.WriteLine("Server wird gestarted...");

            ServiceHost sh = new ServiceHost(typeof(HostMessageService));
            sh.Open();

            if (Datalib.Vars.Verbose)
                Console.WriteLine("Server erfolgreich gestartet...\nHelp für Hilfe");

            string s = "";
            do
            {
                switch (s.ToLower())
                {
                    case "help":
                        Console.WriteLine("\nHelp:\n\nquit, q\tBeendet den Server");
                        break;
                    default:
                        break;
                }

                Console.Write("\nHost> ");
            } while ((s = Console.ReadLine()).ToLower() != "quit" && s.ToLower() != "q");

            if (Datalib.Vars.Verbose)
                Console.WriteLine("Server beenden...");

            sh.Close();

            if (Datalib.Vars.Verbose)
                Console.WriteLine("Server beendet...");
        }

        private static void printHelp(string launchPath)
        {
            Console.WriteLine(launchPath + "\n\t-v\tVerbose (Giebt mehr Informationen aus)\n\t-?\tGiebt diese Hilfe aus");
        }

        private static bool restartAsAdmin(string[] args)
        {
            WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
            if (!hasAdministrativeRight)
            {
                try
                {
                    if (Datalib.Vars.Verbose)
                    {
                        Console.WriteLine("Keine Admin-Rechte :(");
                        Console.WriteLine("Server mit Admin-Rechten neu starten...");
                    }
                    ProcessStartInfo processInfo = new ProcessStartInfo();
                    processInfo.Verb = "runas";
                    processInfo.FileName = Application.ExecutablePath;
                    processInfo.Arguments = "";
                    foreach (string s in args)
                    {
                        processInfo.Arguments += " " + s;
                    }
                    Process.Start(processInfo);
                    if (Datalib.Vars.Verbose)
                        Console.ReadKey();
                }
                catch (Win32Exception)
                {
                    Console.Error.WriteLine("Server kann ohne Admin-Rechte nicht gestartet werden...");
                }
                return true;
            }
            return false;
        }
    }
}
